package com.zh.yida.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.zh.yida.common.BaseContext;
import com.zh.yida.common.R;
import com.zh.yida.entity.ShoppingCart;
import com.zh.yida.service.ShoppingCartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Autowired
    private ShoppingCartService shoppingCartService;

    /**
     * 购物车菜品查询
     * @return
     */
    @GetMapping("/list")
    public R<List<ShoppingCart>> list() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getThreadLocal());
        List<ShoppingCart> shoppingCartList = shoppingCartService.list(lqw);
        return R.success(shoppingCartList);
    }

    /**
     * 购物车添加
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> list(@RequestBody ShoppingCart shoppingCart) {
        shoppingCart.setUserId(BaseContext.getThreadLocal());
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getThreadLocal());
        if (shoppingCart.getDishId() == null) {
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        } else {
            lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        }
        ShoppingCart cartOne = shoppingCartService.getOne(lqw);
        if (cartOne != null) {
            cartOne.setNumber(cartOne.getNumber() + 1);
            shoppingCartService.updateById(cartOne);
        } else {
            shoppingCart.setNumber(1);
            shoppingCartService.save(shoppingCart);
            cartOne = shoppingCart;
        }
        return R.success(cartOne);
    }

    /**
     * 清空菜单
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> remove() {
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        lqw.eq(ShoppingCart::getUserId, BaseContext.getThreadLocal());
        shoppingCartService.remove(lqw);
        return R.success("删除成功");
    }

    @PostMapping("/sub")
    public R<String> sub(@RequestBody ShoppingCart shoppingCart) {
        Long dishId = shoppingCart.getDishId();
        LambdaQueryWrapper<ShoppingCart> lqw = new LambdaQueryWrapper<>();
        if (dishId == null) {
            lqw.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        } else {
            lqw.eq(ShoppingCart::getDishId, shoppingCart.getDishId());
        }
        ShoppingCart one = shoppingCartService.getOne(lqw);
        Integer number = one.getNumber();
        if (number == 1) {
            shoppingCartService.removeById(one);
        } else {
            one.setNumber(number - 1);
            shoppingCartService.updateById(one);
        }

        return R.success("减少成功");
    }
}
